<template>
    <div class="coupon-list">
        <div class="coupon-container" v-if="coupons.length > 0">
            <div class="coupon-item" v-for="coupon in coupons" :key="coupon.id">
                <div class="coupon-left">
                    <div class="amount">¥{{ coupon.value }}</div>
                    <div class="condition">满{{ coupon.minAmount }}元可用</div>
                </div>
                <div class="coupon-right">
                    <div class="title">{{ coupon.name }}</div>
                    <div class="time">有效期：{{ formatDate(coupon.startTime) }} 至 {{ formatDate(coupon.endTime) }}</div>
                    <div class="limit" v-if="coupon.perUserLimit">每人限领{{ coupon.perUserLimit }}张</div>
                    <div class="btn" 
                         :class="{ 'received': coupon.received }"
                         @click="receiveCoupon(coupon.id)"
                         v-if="!coupon.received">
                        立即领取
                    </div>
                    <div class="btn received" v-else>
                        已领取
                    </div>
                </div>
            </div>
        </div>
        <div class="empty" v-else>
            暂无优惠券
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getShopCoupon, receiveCouponApi } from '@/api/coupon'
import { ElMessage } from 'element-plus'
import { useRoute } from 'vue-router'

const route = useRoute()
const coupons = ref([])

// 格式化日期
const formatDate = (date) => {
    if (!date) return ''
    return new Date(date).toLocaleDateString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit'
    })
}

// 获取优惠券列表
const fetchCoupons = async () => {
    try {
        const shopId = route.params.id
        const res = await getShopCoupon(shopId)
        coupons.value = res || []
    } catch (error) {
        console.error('获取优惠券失败:', error)
    }
}

// 领取优惠券
const receiveCoupon = async (couponId) => {
    try {
        await receiveCouponApi(couponId)
        // 更新优惠券状态
        const index = coupons.value.findIndex(c => c.id === couponId)
        if (index !== -1) {
            coupons.value[index].received = true
        }
        ElMessage.success('领取成功')
    } catch (error) {
        console.error('领取优惠券失败:', error)
        ElMessage.error(error.message || '领取优惠券失败')
    }
}

onMounted(async () => {
    await fetchCoupons()
})
</script>

<style scoped>
.coupon-list {
    padding: 10px;
    background: #f5f5f5;
}

.coupon-container {
    display: flex;
    overflow-x: auto;
    padding: 10px 0;
    gap: 10px;
}

.coupon-item {
    display: flex;
    min-width: 280px;
    height: 120px;
    background: #fff;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.coupon-left {
    width: 100px;
    background: #ff4d4f;
    color: #fff;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 10px;
}

.amount {
    font-size: 24px;
    font-weight: bold;
}

.condition {
    font-size: 12px;
    margin-top: 5px;
}

.coupon-right {
    flex: 1;
    padding: 15px;
    position: relative;
}

.title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 10px;
}

.time {
    font-size: 12px;
    color: #999;
    margin-bottom: 5px;
}

.limit {
    font-size: 12px;
    color: #ff4d4f;
    margin-bottom: 5px;
}

.btn {
    position: absolute;
    right: 15px;
    bottom: 15px;
    padding: 5px 15px;
    background: #ff4d4f;
    color: #fff;
    border-radius: 15px;
    font-size: 14px;
    cursor: pointer;
}

.btn:hover {
    opacity: 0.9;
}

.btn.received {
    background: #999;
    cursor: not-allowed;
}

.empty {
    text-align: center;
    padding: 20px;
    color: #999;
}
</style>


